package com.example.demo.controller;


import com.alibaba.fastjson.JSON;
import com.example.demo.entity.fin;
import com.example.demo.entity.finance;
import com.example.demo.entity.inventory;
import com.example.demo.entity.user;
import com.example.demo.service.financeserviceInter;
import com.example.demo.service.superuserserviceInter;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class financecontroller {
    @Autowired
    financeserviceInter financedao;
    @Autowired
    superuserserviceInter superuserdao;

    @RequestMapping("/search")
    public PageInfo<finance> searchfinance(@RequestParam(value = "pagenum", required = false, defaultValue = "1")Integer pagenum,
                                           @RequestParam(value = "pagesize", required = false, defaultValue = "5")Integer pagesize,
                                           finance f,
                                           Model model) {
        PageInfo<finance> finpage = financedao.searchfinance(pagenum, pagesize, f);
        model.addAttribute("finpage", finpage);

        return finpage;
    }

    @RequestMapping("/signdel")
    public int signdel(finance f) {
        return financedao.signdel(f);
    }

    @RequestMapping("/unsigndel")
    public int unsigndel(finance f) {
        return financedao.unsigndel(f);
    }

    @RequestMapping("/sumprice")
    public double sumprice(finance f) {

        double totalprice = 0;
        List<finance> finlist = financedao.sumprice(f);
        Iterator<finance> it = finlist.iterator();

        while(it.hasNext()) {
           totalprice = totalprice + it.next().getTotalprice();
        }

        return totalprice;
    }

    @RequestMapping("/verify")
    public int insertfinance(double totalprice, String finedate, user u) {
        user user = superuserdao.searchid(u);
        int userid = user.getId();
        int bool1 = 0;
        int bool2 = 0;
        int bool = 0;
        fin fin = new fin();
        finance finance = new finance();

        fin.setUserid(userid);
        fin.setVerifydata(finedate);
        fin.setTotalprice(totalprice);
        finance.setFinancedata(finedate);

        bool1 = financedao.insertfinance(fin);

        if (bool1 == 1) {
            bool2 = financedao.signverify(finance);

            if (bool2 != 0) {
                bool = 1;
            }
        }


        return bool;
    }

}
